﻿//********************************************************************
// Author:      刘艳明
// Create Date: 2012-08-25
// Description: 
//
// Version:     1.0.0.0
// Change History:
// Date		    Who		      Changes Made
//--------------------------------------------------------------------
// Please Insert the Modiy record here!
//--------------------------------------------------------------------
//********************************************************************


using System;
using System.Collections.Generic;
using System.Data;

using MillerApp.DataAccess;
using MillerApp.DataContract.MillerDB;
using Npgsql;
using NpgsqlTypes;
using MillerApp.CommonLib.DataAccess;

namespace MillerApp.DataAccess.MillerDB
{
    /// <summary>
    /// 这个类提供了对Commontype ()表的数据库操作
    /// </summary>
    public static partial class CommontypeManager
    {
        private static IDBHelper db = MillerAppDBHelper.CreateDatabase();

        static string insertSQL = "INSERT INTO commontype (res1,description,itemname,parentcode,itemcode,typename) VALUES (@res1,@description,@itemname,@parentcode,@itemcode,@typename)";

        static string updateSQL = "Update commontype Set res1=@res1,description=@description,itemname=@itemname,parentcode=@parentcode,itemcode=@itemcode,typename=@typename Where id=@id";

        static string deleteSQL = "DELETE FROM  commontype  Where id=@id";

        #region codesmith生成的代码
        /// <summary>
        /// 获取CommontypeEntity实体 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>CommontypeEntity</returns>
        public static CommontypeEntity GetCommontypeEntity(int id)
        {
            string strSQL = "select * from commontype where id=@id";
            IDbDataParameter para_id = new NpgsqlParameter("id", NpgsqlDbType.Integer);
            para_id.Value = id;
            IDbDataParameter[] paras = new IDbDataParameter[] { para_id };


            CommontypeEntity entity = new CommontypeEntity();
            using (System.Data.IDataReader reader = db.ExecuteReader(strSQL, paras))
            {
                if (reader.Read() == false) return null;
                LoadFromReader(reader, ref entity);
                return entity;
            }
        }

        /// <summary>
        /// 更新CommontypeEntity
        /// </summary>
        /// <param name="entity">Entity</param>	
        public static void UpdateCommontype(CommontypeEntity entity)
        {
            if (entity == null)
            {
                throw new Exception("参数[entity]为[null]！");
            }
            IDbDataParameter[] paras = GenerateParas(entity);
            db.ExecuteNonQuery(updateSQL, paras);
        }




        /// <summary>
        ///  插入一条记录
        /// </summary>
        /// <param name="id">id</param>		
        public static void InsertCommontype(CommontypeEntity entity)
        {
            if (entity == null)
            {
                throw new Exception("参数[entity]为[null]！");
            }
            IDbDataParameter[] paras = GenerateParas(entity);
            db.ExecuteNonQuery(insertSQL, paras);
        }

        /// <summary>
        ///  删除一条记录
        /// </summary>
        /// <param name="id">id</param>		
        public static void DeleteCommontype(int id)
        {
            IDbDataParameter para_id = new NpgsqlParameter("id", NpgsqlDbType.Integer);
            para_id.Value = id;
            IDbDataParameter[] paras = new IDbDataParameter[] { para_id };
            db.ExecuteNonQuery(deleteSQL, paras);
        }



        /// <summary>
        /// 删除多条记录
        /// </summary>
        /// <param name="ids">ids</param>		
        public static void DeleteCommontype(List<int> ids)
        {
            string strSQL = "DELETE FROM  commontype  Where id in ({0}) ";
            string deleteIds = DBHelper.ConvertListToSQLString<int>(ids);
            db.ExecuteNonQuery(string.Format(strSQL, deleteIds), null);
        }



        /// <summary>
        /// 根据分页条件获取Commontype数据集
        /// </summary>
        /// <param name="conditions">条件</param>	
        /// <param name="sortExpression">排序表达式，为空则默认为:id</param>		
        /// <param name="pageIndex">开始页码：分页查询中的开始页码</param>
        /// <param name="pageSize">页宽：分页查询中的每页的记录数</param>
        /// <param name="recordCount">页宽：分页查询中的每页的记录数</param>		
        /// <returns>数据集(List)</returns>
        public static List<CommontypeEntity> GetCommontypesByCond(string conditions, string sortExpression, int pageIndex, int pageSize, out int recordCount)
        {
            string viewName = "commontype";
            return db.GetDataListByCond<CommontypeEntity>
                (viewName, conditions, sortExpression, pageIndex, pageSize, out recordCount, LoadFromReader);
        }


        /// <summary>
        /// 根据条件获取Commontype数据集
        /// </summary>
        /// <param name="fields">查询的列(如果为空，则表示查询所有列)</param>	
        /// <param name="conditions">条件</param>	
        /// <param name="sortExpression">排序表达式，为空则默认为:id</param>		
        /// <returns>数据集(DataTable)：<br/></returns>
        public static DataTable GetCommontypesByCond(string fields, string conditions, string sortExpression)
        {
            string viewName = "commontype";
            return db.GetDataTableByCond(viewName, fields, conditions, sortExpression);
        }
        #region private method
        /// <summary>
        /// 获取CommontypeEntity实体数据更新参数
        /// </summary>
        /// <param name="entity">CompanyEntity</param>
        /// <returns></returns>
        private static IDbDataParameter[] GenerateParas(CommontypeEntity entity)
        {
            IDbDataParameter para_id = new NpgsqlParameter("id", NpgsqlDbType.Integer);
            para_id.Value = entity.id;

            IDbDataParameter para_res1 = new NpgsqlParameter("res1", NpgsqlDbType.Varchar);
            para_res1.Value = entity.res1;

            IDbDataParameter para_description = new NpgsqlParameter("description", NpgsqlDbType.Varchar);
            para_description.Value = entity.description;

            IDbDataParameter para_itemname = new NpgsqlParameter("itemname", NpgsqlDbType.Varchar);
            para_itemname.Value = entity.itemname;

            IDbDataParameter para_parentcode = new NpgsqlParameter("parentcode", NpgsqlDbType.Varchar);
            para_parentcode.Value = entity.parentcode;

            IDbDataParameter para_itemcode = new NpgsqlParameter("itemcode", NpgsqlDbType.Varchar);
            para_itemcode.Value = entity.itemcode;

            IDbDataParameter para_typename = new NpgsqlParameter("typename", NpgsqlDbType.Varchar);
            para_typename.Value = entity.typename;

            return new IDbDataParameter[] { para_id, para_res1, para_description, para_itemname, para_parentcode, para_itemcode, para_typename };
        }

        /// <summary>
        /// 绑定数据到业务实体
        /// </summary>
        /// <param name="reader">DataReader</param>
        /// <param name="entity">业务实体</param>
        private static void LoadFromReader(IDataReader reader, ref CommontypeEntity entity)
        {
            if (reader != null && !reader.IsClosed)
            {
                //entity.Id = reader.GetInt32(0);
                if (!Convert.IsDBNull(reader["id"])) entity.id = (int)reader["id"];
                if (!Convert.IsDBNull(reader["res1"])) entity.res1 = reader["res1"].ToString();
                if (!Convert.IsDBNull(reader["description"])) entity.description = reader["description"].ToString();
                if (!Convert.IsDBNull(reader["itemname"])) entity.itemname = reader["itemname"].ToString();
                if (!Convert.IsDBNull(reader["parentcode"])) entity.parentcode = reader["parentcode"].ToString();
                if (!Convert.IsDBNull(reader["itemcode"])) entity.itemcode = reader["itemcode"].ToString();
                if (!Convert.IsDBNull(reader["typename"])) entity.typename = reader["typename"].ToString();
            }

        }
        #endregion

        #endregion

        #region 手工增加的代码

        #endregion
    }
}
